// roleService.js
import http from '@/utils/http';
import storage from "@/utils/storage"; // 确保导入了你创建的 axios 实例

export function getRole() {
    if (storage.get('Token')){
        return http({
            url: "/get-role",
            method: "get"
        }).then(({ data }) => {
            if (data && data.code === 0) {
                return data.data; // 返回角色数据
            } else {
                console.error("获取角色失败", data.message);
                throw new Error(data.message || "获取角色失败");
            }
        }).catch(error => {
            console.error("请求出错", error);
            throw error; // 重新抛出错误以便上层处理
        });
    }else {
        return null
    }
}

export function getTokenRole(role){
    if (role === 'admin'){
        return http({
            url: `${role}/token`,
            method: "get",
        }).then(({ data }) => {
                if (data && data.code === 0) {
                    return {
                        username: storage.get("userName"),
                        //useravator: data.data.useravator,
                        password: data.data.adminpassword
                    };
                } else {
                    console.error("获取角色失败", data.message);
                    throw new Error(data.message || "获取角色失败");
                }
            })
            .catch((error) => {
                throw error; // 再次抛出错误以便调用方处理
            });
    }else if (role === 'student'){
        return http({
            url: `${role}/token`,
            method: "get",
        })
            .then(({ data }) => {
                if (data && data.code === 0) {
                    return {
                        username: data.data.studentname,
                        //useravator: data.data.useravator,
                        password: data.data.studentpassword,
                        account: data.data.studentaccount,
                        gender: data.data.gender,
                        classroom: data.data.studentclass,
                        college: data.data.studentcollege
                    };
                } else {
                    console.error("获取角色失败", data.message);
                    throw new Error(data.message || "获取角色失败");
                }
            })
            .catch((error) => {
                throw error; // 再次抛出错误以便调用方处理
            });
    }else if (role === 'mentor'){
        return http({
            url: `${role}/token`,
            method: "get",
        })
            .then(({ data }) => {
                if (data && data.code === 0) {
                    return {
                        username: data.data.mentorname,
                        password: data.data.mentorpassword,
                        account: data.data.mentoraccount,
                        college: data.data.studentcollege,
                        gender: data.data.gender,
                    };
                } else {
                    console.error("获取角色失败", data.message);
                    throw new Error(data.message || "获取角色失败");
                }
            })
            .catch((error) => {
                throw error; // 再次抛出错误以便调用方处理
            });
    }

}
